package jdbc;

import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class window extends JFrame {

	private JPanel contentPane;
	private JTextArea input ;
	private JTextArea output;
	/**
	 * Launch the application.
	 */
	

	/**
	 * Create the frame.
	 */
	public window() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		JButton search = new JButton("查找");
		search.setBounds(223, 69, 157, 39);
		contentPane.add(search);	
		input = new JTextArea();
		input.setBounds(32, 67, 174, 44);
		contentPane.add(input);
		input.setColumns(10);
		JTextArea output = new JTextArea();
		output.setBounds(32, 121, 200, 100);
		contentPane.add(output);							
		search.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
		    		
		    		String str=input.getText();;
		    		String str1="";
		    		String str2;	
		    		PreparedStatement presql;
		    		char ch[]=str.toCharArray();
					Class.forName("com.mysql.jdbc.Driver");				      	   
					Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/student?useUnicode=true&characterEncoding=gbk","root","");		
				   	 
				    ResultSet rs;
					boolean flag = true;
				    if(ch[0]>'A'&&ch[0]<'z') 
				    {
				      presql=con.prepareStatement("select * from dictionary where english=?");
				      presql.setString(1,str);
				      rs=presql.executeQuery();
			    
				      while(rs.next()) {
				    	  flag = false;
				    	  str2=rs.getString(2);
				    	  str1=str2;			  
				       }
				      output.setText("翻译："+str1);
				    }
				    else {
				    	presql=con.prepareStatement("select * from dictionary where chinese=?");
					      presql.setString(1,str);
					      rs=presql.executeQuery();					
					      while(rs.next()) {
					    	  flag = false;
					    	  str2=rs.getString(1);
					    	  str1=str2;	
					    	
					      }
					      output.setText("翻译："+str1);
					      
				    }
				    if(flag) {
						output.setText("没找到!");
						
					}
				 
		    	  } catch (ClassNotFoundException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
		    	  catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}   	 
			}
		});
		
	}
	
}
